<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\index\model\MarkModel;
use app\index\model\TestaModel;
use app\index\model\TestasModel;
use app\index\model\TestbsModel;
use app\index\model\TestcsModel;
use app\index\model\ResultModel;
use app\index\model\StudentModel;
use app\index\model\TestModel;
use app\index\model\User;

class Mark extends Controller
{
    function __construct() {

        parent::__construct();

        $isLogin = Session::get('isLogin');
        if (1 != $isLogin){
            return $this->redirect('/index/index');
        }
    }

    public function index()
    {
        $admin = Session::get('nid');
        $student = StudentModel::get($admin);
        $data['student']  =  $student;
        $number =Session::get('number');
        $test = TestModel::getByNumber($number);
        $data['test']  =  $test;
        $data['number']  =  $number;
        $markas = TestasModel::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        $markbs = TestbsModel::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        $markcs = TestcsModel::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        if ($markas==null){
            $markass = 0;
        } else {
            $markass = count($markas);
        }
        if ($markbs==null){
            $markbss = 0;
        } else {
            $markbss = count($markbs);
        }
        if ($markcs==null){
            $markcss = 0;
        } else {
            $markcss = count($markcs);
        }
        $results = ResultModel::getByNumber($number);
        $a = $results->infoa;
        $marka = $a*$markass;
        $data['marka']  =  $marka;
        $b = $results->infob;
        $markb = $b*$markbss;
        $data['markb']  =  $markb;
        $c = $results->infoc;
        $markc = $c*$markcss;
        $data['markc']  =  $markc;

        $markas = MarkModel::all(['number'=>$number,'admin'=>$admin]);
        $data['markas']  =  $markas;

        $can = Session::get('can');
        $cans = Session::get('cans');
        if($cans==null) {
            $join = $can;
        } else{
            $join = $cans;
        }
        $data['join']  =  $join;

        return $this->fetch('/mark/index',$data);

    }

    public function doAdd()
    {
        $number = Request::instance()->post('number');
        Session::set('number', $number);
        $title = Request::instance()->post('title');
        $admin = Request::instance()->post('admin');
        $timea = Request::instance()->post('timea');
        $list = new MarkModel();
        $lists = $list::get(['number'=>$number,'admin'=>$admin]);
        $mark = new MarkModel;
        if($lists == ''){

            $marks = [
                ['number' => $number,'title'=>$title, 'admin' => $admin,'timea' => $timea]
            ];
            $mark->saveAll($marks);
            return $this->redirect('/index/main/infoa');
        }
        else {
            if($lists->mark == ''){
                return $this->redirect('/index/main/infoa');
            }
            else{
                $can="你已参加过本次考试 考试成绩如下";
                Session::set('can', $can);
                Session::set('cans', "");
                return $this->redirect('/index/mark/index');
            }
        }
    }
    public function doad()
    {
        $admin = Session::get('nid');
        $number =Session::get('number');
        $marka = new TestasModel();
        $markas = $marka::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        $markb = new TestbsModel();
        $markbs = $markb::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        $markc = new TestcsModel();
        $markcs = $markc::all(['number'=>$number,'admin'=>$admin,'score'=> '1']);
        if ($markas==null){
            $markass = 0;
        } else {
            $markass = count($markas);
        }
        if ($markbs==null){
            $markbss = 0;
        } else {
            $markbss = count($markbs);
        }
        if ($markcs==null){
            $markcss = 0;
        } else {
            $markcss = count($markcs);
        }
        $results = ResultModel::getByNumber($number);
        $a = $results->infoa;
        $b = $results->infob;
        $c = $results->infoc;
        $markss = ($markass*$a) + ($markbss*$b) + ($markcss*$c);
        $list = new MarkModel();
        $lists = $list::get(['number'=>$number,'admin'=>$admin]);
        $id = $lists->id;
        $mark = new MarkModel;
        $mark->where('id', $id)
            ->update(['mark' => $markss]);

        $cans = "本次考试成绩如下";
        Session::set('cans', $cans);
        Session::set('can', "");
        return $this->redirect('/index/mark/index');
    }

    public function doads()
    {
        $admin = Session::get('nid');
        $number =Session::get('number');
        $markc = Request::instance()->post('markc');
        $markd = Request::instance()->post('markd');
        $marks = $markc + $markd;
        $list = new MarkModel();
        $lists = $list::get(['number'=>$number,'admin'=>$admin]);
        $id = $lists->id;
        $mark = new MarkModel;
        $mark->where('id', $id)
            ->update(['marks' => $marks]);
    }
}
